package com.egame.manage.biz.server.gc;

import java.util.Date;
import java.util.List;

import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.utils.AndroidInfoUtils;

import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.EnumType.FileStatus;
import cn.egame.interfaces.fl.FileInfo;
import cn.egame.interfaces.gc.GameFileInfo;

public class GameFileServiceDao extends BaseDao {
	public GameFileServiceDao() throws ExceptionCommonBase {
		super("egame_game");
	}
	
	public int updateFileStatus(long fileId,int status) throws ExceptionCommonBase{
		String sql = "update t_game_file set file_status = " + status + " where file_id = " + fileId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}

	public int setFileRemark(long fileId,String remark) throws ExceptionCommonBase{
		if(!Utils.stringIsNullOrEmpty(remark)){
			String sql = "update t_game_file set remark = " + SqlUtils.QuataStr(remark) + " where file_id = " + fileId;
			return this.executeUpdate(sql);
		}
		return -1;
	}

	public int deleteGameFileById(long fid)throws ExceptionCommonBase{
		String sql = "delete from t_game_file where file_id = " + fid;
		return this.executeUpdate(sql);
	}

	public long setGameFile(int appId,long loginUserId,long efsid,int gid,FileInfo file)throws Exception{
		String filepath = cn.egame.interfaces.ConstVar.UPLOAD_ADDRESS + GameServiceImpl.getInstance().getLocalFilePath(appId, loginUserId, efsid);	
		AndroidInfoUtils androidInfo = new AndroidInfoUtils();
		String[] arrInfo = androidInfo.parseAndroidInfo(filepath);
		GameFileInfo bean = new GameFileInfo();
		bean.setFileName(file.getFileName());
		bean.setgId(gid);
		bean.setEfsId(efsid);
		bean.setFileSize(file.getFileSize());
		bean.setVersion("1");
		bean.setPackageName(arrInfo[0]);
		bean.setVersionName(arrInfo[1]);
		bean.setVersionCode(arrInfo[2]);
		bean.setFileStatus(FileStatus.none);
		bean.setOnlineTime(new Date(0));
		bean.setOfflineTime(new Date(0));
		bean.setOperatorId((int)loginUserId);
		bean.setInsertTime(new Date().getTime());
		bean.setUpdateTime(new Date(0).getTime());
		System.out.println(bean.toString());
		ManageClient.getInstance().setGameFileInfo(appId, loginUserId, bean);
		return getFileIdByEfsId(efsid);
	}
	
	public long getFileIdByEfsId(long efsId)throws ExceptionCommonBase{
		String sql = "select file_id from t_game_file WHERE efs_id = " + efsId;
		return this.getLong(sql);
	}

	public int getFileCountByGId(int gId) throws ExceptionCommonBase{
		String sql = "select count(*) from t_game_file where g_id = " + gId;
		System.out.println(sql);
		return this.getInt(sql);
	}

	public int getFileLinkCountByGid(int gId) throws ExceptionCommonBase{
		String sql = "select count( DISTINCT file_id) from t_terminal_game_file_link where g_id = " + gId;
		System.out.println(sql);
		return this.getInt(sql);
	}

	public List<Integer> listGameFileIdsByGid(int gid)throws ExceptionCommonBase{
		String sql = "select file_id from t_game_file where g_id = " + gid;
		return this.getListInteger(sql);
	}
//
//	public long getAdaptFile(int gid)throws ExceptionCommonBase{
//		String sql = "SELECT file_id from t_game_file where g_id = " + gid + " and file_status = " + FileStatus.adaptationed.value();
//		return this.getLong(sql);
//	}

	public boolean hasAdapted(int gid,int terminalId)throws ExceptionCommonBase{
		String sql = "select count(*) from t_terminal_game_file_link where g_id = " + gid+ " and terminal_id = " + terminalId;
		int count = this.getInt(sql);
		System.out.println(sql);
		return count>0;
	}
	
	public List<String[]> listPackageName(int gid)throws ExceptionCommonBase{
		String sql = "select g_id,package_name from t_game_file where file_status = 3 and package_name is not null and package_name != '' ";
		if(gid>0){
			sql += " and g_id = " + gid;
		}
		System.out.println(sql);
		return this.getListStrings(sql);
	}
}
